import java.util.Stack;

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;
    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    // 入栈
    public void push(int val) {
        stack.push(val);
        // 第一次往最小栈里放元素
        if(minStack.empty()) {
            minStack.push(val);
        }else {
            if(val <= minStack.peek()) {
                minStack.push(val);
            }
        }
    }
    // 出栈
    public void pop() {
        // 栈为空 不能弹出元素
        if (stack.empty()) {
            return;
        }
        int val = stack.pop();
        if(val == minStack.peek()) {
            minStack.pop();
        }
    }
    // 获取栈顶元素 和最小栈没有关系
    public int top() {
        if (stack.empty()) {
            return -1;
        }
        return stack.peek();
    }
    
    public int getMin() {
        return minStack.pop();
    }
    public static void main(String[] args) {
        MinStack minStack1 = new MinStack();
        minStack1.push(-2);
        minStack1.push(0);
        minStack1.push(1);
        int x1 = minStack1.getMin();
        int x2 = minStack1.top();
        minStack1.pop();
        int x4 = minStack1.getMin();
    }
}